import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/index',
    meta: {
      isLogin: true
    },
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('../views/one/index.vue'),
        redirect: '/index/houtai',
        meta: {
          isLogin: true
        },
        children: [
          {
            path: 'houtai',
            name: 'houtai',
            component: () => import('../views/two/houtai.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'photo',
            name: 'photo',
            component: () => import('../views/two/photo.vue'),
            meta: {
              isLogin: true
            },
          },
        ]
      },
      {
        path: 'shop',
        name: 'shop',
        component: () => import('../views/one/shop.vue'),
        meta: {
          isLogin: true
        },
        redirect: '/shop/shopList',
        children: [
          {
            path: 'shopList',
            name: 'shopList',
            component: () => import('../views/two/shopList.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'fenlei',
            name: 'fenlei',
            component: () => import('../views/two/fenlei.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'standard',
            name: 'standart',
            component: () => import('../views/two/standard.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'shopType',
            name: 'shopType',
            component: () => import('../views/two/shopType.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'pinglun',
            name: 'pinglun',
            component: () => import('../views/two/pinglun.vue'),
            meta: {
              isLogin: true
            },
          }
        ]
      },
      {
        path: 'vip',
        name: 'vip',
        component: () => import('../views/one/vip.vue'),
        meta: {
          isLogin: true
        },
        redirect: '/vip/vipList',
        children: [
          {
            path: 'vipList',
            name: 'vipList',
            component: () => import('../views/two/vipList.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'vipdengji',
            name: 'vipdengji',
            component: () => import('../views/two/vipdengji.vue'),
            meta: {
              isLogin: true
            },
          }
        ]
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('../views/one/order.vue'),
        meta: {
          isLogin: true
        },
        redirect: '/order/orderguanli',
        children: [
          {
            path: 'orderguanli',
            name: 'orderguanli',
            component: () => import('../views/two/orderguanli.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'fapiao',
            name: 'fapiao',
            component: () => import('../views/two/fapiao.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'shouhou',
            name: 'shouhou',
            component: () => import('../views/two/shouhou.vue'),
            meta: {
              isLogin: true
            },
          }
        ]
      },
      {
        path: 'setting',
        name: 'setting',
        component: () => import('../views/one/setting.vue'),
        meta: {
          isLogin: true
        },
        redirect: '/setting/jichushezhi',
        children: [
          {
            path: 'jichushezhi',
            name: 'jichushezhi',
            component: () => import('../views/two/jichushezhi.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'wuliu',
            name: 'wuliu',
            component: () => import('../views/two/wuliu.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'guanliyuan',
            name: 'guanliyuan',
            component: () => import('../views/two/guanliyuan.vue'),
            meta: {
              isLogin: true
            },
          },
          {
            path: 'jiaoyi',
            name: 'jiaoyi',
            component: () => import('../views/two/jiaoyi.vue'),
            meta: {
              isLogin: true
            },
          }
        ]
      },
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/one/login.vue')
  }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  if(to.matched.some(route => route.meta.isLogin)) {
    var token = localStorage.getItem('user')
    if(!token) {
      if(to.path == '/login') {
        next()
      } else {
      next('/login')
      alert('清先登录')
      }
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
